package threadexercise;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPoolDemo {
    public static void main(String[] args) {
        ExecutorService pool= Executors.newFixedThreadPool(2);
        for (int i=0;i<5;i++){
            Runnable r=new Runnable() {
                @Override
                public void run() {
                    Thread t=Thread.currentThread();//获取运行该任务的主线程
                    System.out.println(t.getName()+"线程任务开始执行");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(t.getName()+"线程任务执行完毕");
                }
            };
            pool.execute(r);//把任务交给线程池
        }
        pool.shutdown();
        System.out.println("线程池停止了");

    }
}
